package com.way.gesture;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureStore;
import android.gesture.Prediction;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.way.util.AppUtil;
import com.way.util.GestureLevel;
import com.way.util.GestureLibraryManager;

public class MainActivity extends Activity implements
		GestureOverlayView.OnGesturePerformedListener {
	private static final String TAG = "MainActivity";
	public static final String GESTURE_FILE = "gestures";
	private GestureLibraryManager mGestureLibraryManager;
	private GestureOverlayView mGestureOverlayView;
	private View mExtraSetView;
	private Gesture mGesture;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mGestureLibraryManager = GestureLibraryManager.getInstance(this);
		// AppUtil.transWindows(this,
		// getResources().getColor(R.color.material_teal));
		mExtraSetView = findViewById(R.id.extra_settings_view);
		mGestureOverlayView = (GestureOverlayView) findViewById(R.id.main_gesture);
		mGestureOverlayView.addOnGesturePerformedListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		// 加载本应用目录下的gestures文件，生成GestureLibrary手势库
		mGestureLibraryManager.load();
		// File storeFile = new File(getFilesDir(), GESTURE_FILE);
		// mGestureLibrary = GestureLibraries.fromFile(storeFile);
		// mGestureLibrary.load();
	}

	@SuppressWarnings({ "UnusedDeclaration" })
	public void onSettingsClick(View v) {
		startActivity(new Intent(MainActivity.this,
				GestureBuilderActivity.class));
		finish();
	}

	@SuppressWarnings({ "UnusedDeclaration" })
	public void onClearClick(View v) {
		mGestureOverlayView.clear(true);
		mExtraSetView.setVisibility(View.GONE);
	}

	@SuppressWarnings({ "UnusedDeclaration" })
	public void onAddClick(View v) {
		Intent intent = new Intent(this, SelectTaskActivity.class);
		intent.putExtra("gesture", mGesture);
		startActivity(intent);
		mExtraSetView.setVisibility(View.GONE);
		mGestureOverlayView.clear(true);
	}

	private void recognizeGesture() {
		String result = mGestureLibraryManager.searchGesture(mGesture);
		if (result == null) {// 没有手势
//			Toast.makeText(MainActivity.this, "没有此手势", Toast.LENGTH_SHORT)
//					.show();
			mExtraSetView.setVisibility(View.VISIBLE);
			return;
		}
		if (result.equals("")) {
			// 手势匹配度太低
			Toast.makeText(MainActivity.this, R.string.notfull,
					Toast.LENGTH_SHORT).show();
			return;
		}
		String[] names = result.split(",");
		int command = Integer.parseInt(names[0]);
		switch (command) {
		case SelectTaskActivity.APP_COMMAND:
			Intent appIntent = new Intent(Intent.ACTION_MAIN);
			appIntent.addCategory(Intent.CATEGORY_LAUNCHER);
			ComponentName componentName = new ComponentName(names[1], names[2]);
			appIntent.setComponent(componentName);
			appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
					| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
			AppUtil.startActivitySafely(MainActivity.this, appIntent);
			break;
		case SelectTaskActivity.DIAL_COMMAND:
			Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ names[1]));
			AppUtil.startActivitySafely(MainActivity.this, intent);
			break;
		case SelectTaskActivity.MMS_COMMAND:
			Uri smsToUri = Uri.parse("smsto:" + names[1]);
			Intent mIntent = new Intent(android.content.Intent.ACTION_SENDTO,
					smsToUri);
			AppUtil.startActivitySafely(MainActivity.this, mIntent);
			break;
		default:
			break;
		}
		finish();

		mGestureOverlayView.clear(true);
	}

	@Override
	public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
		if (gesture == null) {
			return;
		}
		if (gesture.getLength() < GestureLevel.MIN_LENGTH) {
			Toast.makeText(MainActivity.this,
					R.string.gesture_message_too_short, Toast.LENGTH_SHORT)
					.show();
			overlay.clear(true);
			return;
		}
		if (gesture.getLength() > GestureLevel.MAX_LENGTH) {
			Toast.makeText(MainActivity.this,
					R.string.gesture_message_too_long, Toast.LENGTH_SHORT)
					.show();
			overlay.clear(true);
			return;
		}
		mGesture = gesture;
		MainActivity.this.runOnUiThread(recognizeTask);
	}

	Runnable recognizeTask = new Runnable() {

		@Override
		public void run() {
			recognizeGesture();
		}
	};
}
